package com.hjm.project.returnBlack.returnBlack.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.returnBlack.returnBlack.domain.ReturnBlackList;
import com.hjm.project.returnBlack.returnBlack.service.IReturnBlackListService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import javax.annotation.Resource;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 返现黑名单Controller
 * 
 * @author maker-platform
 * @date 2023-01-05
 */
@Controller
@RequestMapping("/returnBlack/returnBlack")
public class ReturnBlackListController extends BaseController {
    private String prefix = "returnBlack/returnBlack";

    @Resource
    private IReturnBlackListService returnBlackListService;

    @RequiresPermissions("returnBlack:returnBlack:view")
    @GetMapping()
    public String returnBlack() {
        return prefix + "/returnBlack";
    }

    /**
     * 查询返现黑名单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReturnBlackList returnBlackList) {
        startPage();
        List<ReturnBlackList> list = returnBlackListService.selectReturnBlackListList(returnBlackList);
        return getDataTable(list);
    }

    /**
     * 导出导入返现黑名单模板
     */
    @Log(title = "返现黑名单", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    @ResponseBody
    public AjaxResult export(ReturnBlackList returnBlackList) {
        List<ReturnBlackList> list = new ArrayList<>();
        ExcelUtil<ReturnBlackList> util = new ExcelUtil<ReturnBlackList>(ReturnBlackList.class);
        return util.exportExcel(list, "returnBlack");
    }

    /**
     * 新增返现黑名单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存返现黑名单
     */
    @RequiresPermissions("returnBlack:returnBlack:add")
    @Log(title = "返现黑名单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ReturnBlackList returnBlackList) {
        int result = returnBlackListService.insertReturnBlackList(returnBlackList,this.getLoginName());
        if(result == -99){
            return AjaxResult.error("身份证号已存在！");
        }else if(result < 1){
            return AjaxResult.error("添加失败！");
        }else{
            return AjaxResult.success("添加成功！");
        }
    }

    /**
     * 修改返现黑名单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        ReturnBlackList returnBlackList = returnBlackListService.selectReturnBlackListById(id);
        mmap.put("returnBlackList", returnBlackList);
        return prefix + "/edit";
    }

    /**
     * 修改保存返现黑名单
     */
    @RequiresPermissions("returnBlack:returnBlack:edit")
    @Log(title = "返现黑名单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReturnBlackList returnBlackList) {
        return toAjax(returnBlackListService.updateReturnBlackList(returnBlackList));
    }

    /**
     * 删除返现黑名单
     */
    @RequiresPermissions("returnBlack:returnBlack:remove")
    @Log(title = "返现黑名单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(returnBlackListService.deleteReturnBlackListByIds(ids));
    }

    /**
     *  查询全部商户
     * @return
     */
    @RequestMapping("selectMerInfo")
    public String selectMerInfo(){
        return "riskControl/riskmerblack/selectMerInfo";
    }

    /**
     * 查看详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        ReturnBlackList returnBlackList = returnBlackListService.selectReturnBlackListById(id);
        mmap.put("returnBlackList", returnBlackList);
        return prefix + "/detail";
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ReturnBlackList> util = new ExcelUtil<>(ReturnBlackList.class);
        List<ReturnBlackList> userList = util.importExcel(file.getInputStream());
        //调用导入
        Map message = returnBlackListService.importReturnBlackList(userList,this.getLoginName());
        if(message.get("msg").toString().equals("0000")){        //导入成功
            return AjaxResult.success(message.get("code").toString());
        }else{        //导入失败
            return AjaxResult.error(message.get("code").toString());

        }
    }

}
